package org.androidpn.server.console.controller;

import org.androidpn.server.xmpp.push.NotificationManager;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 服务端发送聊天消息
 *
 * @anthor huang
 * @Create 2017/5/24
 **/
public class SendChatMsgController extends MultiActionController {

    private NotificationManager notificationManager;

    public SendChatMsgController() {
        notificationManager = new NotificationManager();
    }

    public ModelAndView list(HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("sendmessage/chat.jsp");
        return mav;
    }

    public ModelAndView chat(HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        System.out.println("Notification Chat.....");
        String toUser = ServletRequestUtils.getStringParameter(request,
                "chatto");
        String fromUser = ServletRequestUtils.getStringParameter(request,
                "chatfrom");
        String message = ServletRequestUtils.getStringParameter(request, "chatmessage");

        notificationManager.sendChatMsgFromWeb(fromUser, toUser, message);

        ModelAndView mav = new ModelAndView();
        mav.setViewName("redirect:sendmsg.do");
        return mav;
    }
}
